package com.jf.apply_car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jf.apply_car.bean.Driver;
import com.jf.apply_car.bean.Userinfo;
import com.jf.apply_car.mapper.DriverMapper;
import com.jf.apply_car.mapper.UserinfoMapper;
import com.jf.apply_car.service.UserinfoService;
import com.jf.apply_car.util.BusinessException;
import com.jf.apply_car.util.LoginUtil;
import com.jf.apply_car.util.MyConstants;
import com.jf.apply_car.util.ResultJson;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Autowired
    private UserinfoMapper userinfoMapper;
    @Autowired
    private DriverMapper driverMapper;

    public Userinfo login(Userinfo userinfo) {
        Userinfo loginInfo = userinfoMapper.getByUsername(userinfo.getUsername());
        if (loginInfo == null || !loginInfo.getPassword().equals(DigestUtils.md5Hex(userinfo.getPassword()))) {
            throw new BusinessException("用户名或密码错误");
        }
        if (loginInfo.getStatus() != MyConstants.STATUS_AVAILABLE) {
            throw new BusinessException("该用户已被封禁，请联系管理员");
        }

        return loginInfo;
    }

    public ResultJson getPage(int page, int limit, Userinfo userinfo) {

        Userinfo loginInfo = LoginUtil.getLoginInfo();
        if (loginInfo.getRoleId() == MyConstants.BOSS_ROLE_ID) {
            userinfo.setDepartmentId(loginInfo.getDepartmentId());
        }

        /*
         * PageHelper.startPage方法一定写在查询方法的前一行
         * */
        PageHelper.startPage(page, limit);
        /*此时查询的方法虽然是查询全部，但是结果是分页数据*/
        List<Userinfo> list = userinfoMapper.getAll(userinfo);
        /*初始化分页信息类，可用获取到总条数*/
        PageInfo<Userinfo> pageInfo = new PageInfo<Userinfo>(list);
        return ResultJson.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public void delete(int[] ids) {
        for (int id : ids) {
            Userinfo userinfo = userinfoMapper.selectByPrimaryKey(id);
            if (userinfo.getRoleId() == MyConstants.ADMIN_ROLE_ID) {
                throw new BusinessException("管理员禁止删除！");
            }
        }
        userinfoMapper.deleteBatch(ids);
    }

    public void saveOrUpdate(Userinfo userinfo, Driver driver) {
        if (userinfo.getUserinfoId() == null) {
            // 重名验证
            Userinfo search = userinfoMapper.getByUsername(userinfo.getUsername());
            if (search != null) {
                throw new BusinessException("用户名已存在！");
            }
            userinfo.setPassword(DigestUtils.md5Hex(MyConstants.DEFAULT_PASSWORD));
            userinfoMapper.insertSelective(userinfo);
            if (userinfo.getRoleId() == MyConstants.DRIVER_ROLE_ID) {
                /*存储司机信息*/
                driver.setUserinfoId(userinfo.getUserinfoId());
                driverMapper.insertSelective(driver);
            }

        } else {
            userinfoMapper.updateByPrimaryKeySelective(userinfo);
            if (userinfo.getRoleId() == MyConstants.DRIVER_ROLE_ID) {
                driverMapper.updateByPrimaryKeySelective(driver);
            }
        }
    }

    public ResultJson getById(int userinfoId) {
        return ResultJson.success(userinfoMapper.selectByPrimaryKey(userinfoId));
    }

    public ResultJson checkPassword(Userinfo userinfo) {
        Userinfo search = userinfoMapper.selectByPrimaryKey(userinfo.getUserinfoId());
        if (search == null || !search.getPassword().equals(DigestUtils.md5Hex(userinfo.getPassword()))) {
            return ResultJson.error("原密码错误");
        }
        return ResultJson.success();
    }

    public void updatePassword(Userinfo userinfo) {
        /*对修改密码单独处理*/
        userinfo.setPassword(DigestUtils.md5Hex(userinfo.getPassword()));
        userinfoMapper.updateByPrimaryKeySelective(userinfo);
    }

    public ResultJson driverSelect() {
        Userinfo userinfo = new Userinfo();
        userinfo.setRoleId(MyConstants.DRIVER_ROLE_ID);
        return ResultJson.success(userinfoMapper.getAll(userinfo));
    }
}
